#!/bin/sh
#
# This script will automates the steps used to producing a static win32
# package of SoX.
#
# It requires cygwin with gcc and zip packages installed.  Also, it
# requires to already have several external libraries already installed
# under /usr/local.
#
# And last, it will optional package a wget compiled with VC++ instead
# of cygwin version (which has no DLL's to distribute) if found
# in wget-1.11.4 directory.  If not found, it will distribute the
# cygwin version and various DLL's it requires.
#
# Various notes:
#
# Script makes use of "-static" option to tell compiler to prefer static
# external libraries so that we do not need to distribute DLL's.
#
# Libtool will get confused with this flag for external libraries
# that have a libtool lib*.la file and support shared libraries as
# well as static libraries (but usually only if that library
# further depends on other external libraries with lib*.la files).
# Libtool may ignore -static option or it may link first external
# library as static but other dependent libraries as shared (usually
# because it follows $dependency_libs and that ignores -static option).
#
# Work arounds include to only install static libraries, delete the lib*.la
# file, or edit the lib*.la file and set dlnames and library_names variables
# to empty string ("").
#
# The following command lines were used to generate the static external
# libraries SoX ships with.
#
# libpng.la will have libtool issue because depends on libz
# which has a libz.la file.  Must edit libpng.la to
# prevent needing to distribute cygzlib1.dll.
# cd libpng-1.2.41
# ./configure --disable-shared --enable-static;make;sudo make install
#
# cd ../wavpack-4.60.1
# ./configure --disable-shared --enable-static;make;sudo make install
#
# cd ../flac-1.2.1
# ./configure --disable-shared --enable-static;make;sudo make install
#
# cd ../libogg-1.1.3
# ./configure --disable-shared --enable-static;make;sudo make install
#
# cd ../libvorbis-1.2.0
# ./configure --disable-shared --enable-static;make;sudo make install
#
# cd ../libsndfile-1.0.20
# ./configure --disable-shared --enable-static;make;sudo make install
#
# cd ../libid3tag-0.15.1b
# ./configure --disable-shared --enable-static;make;sudo make install
#
# To get MP3 header files used to enable MP3 support (no libraries used):
#
# cd ../libmad
# ./configure --enable-shared --disable-static;make;sudo make install

[ ! -x configure ] && autoreconf -i

# Some versions of autoconf (2.63?) seem to get easily confused about
# CPP variable. If you see warning messages about header files
# rejected by preprocessor then its most likely from that.
# Force the value of CPP=cpp works around that bug.
# static versions of libsndfile do not advertise when they have
# FLAC or ogg vorbis support.  Need to force the link ourselves.
if [ $# -ne 0 -o ! -r Makefile ]; then
  ./configure \
    --disable-shared \
    --with-libltdl \
    --enable-dl-lame --enable-dl-mad --enable-dl-amrnb --enable-dl-amrwb \
    CC=gcc CPP=cpp\
    CPPFLAGS=-I/usr/local/include \
    LDFLAGS="-L/usr/local/lib" \
    SNDFILE_LIBS="-lsndfile -lFLAC -lvorbisenc -lvorbisfile -lvorbis -logg" \
    $*
fi

make -s all pdf txt || exit 1

dir=sox-`grep Version: sox.pc | cut -d ' ' -f 2`
rm -rf $dir $dir-cygwin32.zip
mkdir -p $dir

for f in ChangeLog LICENSE.GPL README README.win32; do
  cp -p $f $dir/$f.txt
  unix2dos $dir/$f.txt
done

binaries=src/sox

[ ! -r "../wget-1.11.4/wget.exe" -a -r /usr/bin/wget ] && binaries+=" /usr/bin/wget"

binaries=$(for f in `cygcheck $binaries|dos2unix`
    do cygpath -u $f; done|sort|uniq|grep -v ^/cygdrive/)

cp -p \
  $binaries \
  sox.pdf \
  soxformat.pdf \
  soxi.pdf \
  scripts/batch-example.bat \
  $dir

unix2dos $dir/batch-example.bat

if test -r "../wget-1.11.4/wget.exe"; then
  cp ../wget-1.11.4/wget.exe $dir
  cp ../wget-1.11.4/wget.ini $dir
else
  if test -r /usr/bin/wget -a -r /etc/wgetrc; then
    cp -p /etc/wgetrc $dir/wget.ini
    chmod +r $dir/wget.ini
  fi
fi

zip -r $dir-cygwin32.zip $dir
rm -rf $dir
